//
//  Store2Goods.m
//  KYRR
//
//  Created by kuyuZJ on 2017/4/20.
//
//

#import "Store2Goods.h"
#import "StoreGoodsTypeCell.h"
#import "StoreGoodsSection.h"
#import "StoreGoodsCell.h"
#import "VerticallyAlignedLabel.h"

#import <MXSegmentedPager/MXSegmentedPager.h>

@interface Store2Goods ()<MXPageProtocol, UITableViewDelegate, UITableViewDataSource,StoreGoodsDelegate>
@property (nonatomic, strong) UITableView *tableLeft;
@property (nonatomic, strong) UITableView *tableRight;
@property(nonatomic,strong) VerticallyAlignedLabel* labelEmpty;
@property(nonatomic,strong) NSString* storeID;

@property(nonatomic,strong) MStoreGoods* entity;

@property(nonatomic,assign) NSInteger currentIndex;


@property(nonatomic,strong) UIView* carView;

@end

static NSString*  const leftCellIdentifier =  @"leftCellIdentifier";

static NSString*  const rightCellIdentifier =  @"rightCellIdentifier";

static NSString* const rightReuseIdentifier =  @"rightReuseIdentifier";

@implementation Store2Goods{
    CGFloat startContentOffsetY,willEndContentOffsetY,endContentOffsetY;
}

-(instancetype)initWithStoreID:(NSString *)storeID{
    self = [super init];
    if(self){
        _storeID = storeID;
        _currentIndex = 0;
        [self addSubview:self.tableLeft];
        [self addSubview:self.tableRight];
        [self addSubview:self.carView];
        [self queryData];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSArray<NSString*>* formats = @[@"H:|-defEdge-[tableLeft(==leftWidth)][tableRight]-defEdge-|",@"H:|-defEdge-[carView]-defEdge-|",
                                    @"V:|-defEdge-[tableLeft][carView(==carHeight)]-defEdge-|", @"V:|-defEdge-[tableRight][carView]"];
    NSDictionary<NSString*,id>* metrics = @{ @"defEdge":@(0), @"leftEdge":@(10), @"topEdge":@(10), @"carHeight":@(45.f),
                                             @"leftWidth":@(SCREEN_WIDTH/4)};
    NSDictionary<NSString*,id>* views = @{ @"tableLeft":self.tableLeft, @"tableRight":self.tableRight , @"carView":self.carView};
    [formats enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //        WMLog( @"%@ %@",[self class],obj);
        NSArray<NSLayoutConstraint*>* constraints = [NSLayoutConstraint constraintsWithVisualFormat:obj options:0 metrics:metrics views:views];
        [self addConstraints:constraints];
    }];
    
}

#pragma mark =====================================================  Data source
-(void)queryData{
    NSDictionary* arg = @{@"ince":@"get_shop_foodlist",@"sid":self.storeID};
    NetRepositories* reposiories = [[NetRepositories alloc]init];
    [reposiories searchStoreGoods:arg complete:^(NSInteger react, MStoreGoods *obj, NSString *message) {
        if(react == 1){
            self.entity = obj;
            for (MGoodsType* item in self.entity.arrayType) {
                for (MGoods* subItem in item.arrayGoods) {
                    subItem.storeStatus = self.entity.shopstatus;
                }
            }
        }else if(react == 400){
            
        }else{
            
        }
        [self.tableLeft reloadData];
        [self.tableRight reloadData];
         [self.tableLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }];
}


#pragma mark =====================================================  <UITableViewDataSource>

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(tableView == self.tableLeft){
        return 1;
    }else{
        return self.entity.arrayType.count;
    }
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.tableLeft){
        return self.entity.arrayType.count;
    }else{
        return self.entity.arrayType[section].arrayGoods.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.tableLeft){
        StoreGoodsTypeCell* cell = [tableView dequeueReusableCellWithIdentifier:leftCellIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell loadDataWithItem:self.entity.arrayType[indexPath.row] isSelected:indexPath.row == self.currentIndex];
        return cell;
    }else{
        StoreGoodsCell* cell = [tableView dequeueReusableCellWithIdentifier:rightCellIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.entity = self.entity.arrayType[indexPath.section].arrayGoods[indexPath.row];
        cell.delegate = self;
        return cell;
    }
}

#pragma mark =====================================================   <UITableViewDelegate>

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == self.tableLeft){
        self.currentIndex = indexPath.row;
        [self.tableRight scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }else{
        MGoods* item = self.entity.arrayType[indexPath.section].arrayGoods[indexPath.row];
        if(self.delegate && [self.delegate respondsToSelector:@selector(store2GoodsDidSelectedItem:)]){
            [self.delegate store2GoodsDidSelectedItem:item];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == self.tableLeft){
        return 60.f;
    }else{
        CGFloat itemSpace = 5.f;
        CGFloat topEdge = 10.f;
        return topEdge + 20.f + itemSpace + 15 + itemSpace + 20.f + topEdge;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(tableView == self.tableLeft){
        return 0.01f;
    }else{
        return 30.f;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(tableView == self.tableRight){
        StoreGoodsSection* reuseView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:rightReuseIdentifier];
        reuseView.entity = self.entity.arrayType[section];
        return reuseView;
    }
    return [UIView new];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView == self.tableRight) {
        NSIndexPath *indexPath = [[self.tableRight indexPathsForVisibleRows ] objectAtIndex:0];
        [self.tableLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.tableRight) {
        NSIndexPath *indexPath = [[self.tableRight indexPathsForVisibleRows ] objectAtIndex:0];
        [self.tableLeft selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

#pragma mark =====================================================  <StoreGoodsDelegate>
-(void)storeGoodsAddCar:(MGoods *)item{
    if(self.delegate && [self.delegate respondsToSelector:@selector(store2GoodsAddCar:)]){
        [self.delegate store2GoodsAddCar:item];
    }
}


#pragma mark =====================================================  property packag

- (UITableView *)tableLeft {
    if (!_tableLeft) {
        _tableLeft = [[UITableView alloc] init];
        _tableLeft.delegate    = self;
        _tableLeft.dataSource  = self;
        _tableLeft.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableLeft registerClass:[StoreGoodsTypeCell class] forCellReuseIdentifier:leftCellIdentifier];
        _tableLeft.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _tableLeft;
}

- (UITableView *)tableRight {
    if (!_tableRight) {
        _tableRight = [[UITableView alloc] init];
        _tableRight.delegate    = self;
        _tableRight.dataSource  = self;
        _tableRight.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableRight registerClass:[StoreGoodsCell class] forCellReuseIdentifier:rightCellIdentifier];
        [_tableRight registerClass:[StoreGoodsSection class] forHeaderFooterViewReuseIdentifier:rightReuseIdentifier];
        _tableRight.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _tableRight;
}

-(UIView *)carView{
    if(!_carView){
        UIView* empty = [[UIView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _carView = empty;
    }
    return _carView;
}

-(VerticallyAlignedLabel *)labelEmpty{
    if(!_labelEmpty){
        VerticallyAlignedLabel* empty = [[VerticallyAlignedLabel alloc]init];
        empty.textColor = theme_Fourm_color;
        empty.font = [UIFont systemFontOfSize:12.f];
        empty.numberOfLines = 2;
        empty.preferredMaxLayoutWidth = SCREEN_WIDTH*3/4-30.f-40.f;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelEmpty = empty;
    }
    return _labelEmpty;
}

@end
